# coding=utf-8

import zipfile
import os

target = r"I:\test0819-10\BetaLog.zip"


def p(str1):
    if 1:
        print("%s" % str1)

        
def extra(path, list_head, next_head):
    path1 = None
    
    # 1
    file = zipfile.ZipFile(path)
    
    list1 = []
    for z in file.infolist():
        for head in list_head:
            if z.filename.startswith(head):
                list1.append(z)
                break
    
    output = file.filename[:-4]
    file.extractall(output, members=list1)
    file.close()
    
    # 2
    if next_head:
        for filename in os.listdir(output):
            if filename.startswith(next_head):
                path1 = os.path.join(output, filename)

    file.close()
    
    return path1


def extra2path(filepath, out):
    file = zipfile.ZipFile(filepath)
    file.extractall(out)

    
if __name__ == '__main__':
    
    # 1
    path1 = extra(target, ['manual'], 'manual')
    if not path1:
        os.system("pause")
    p(path1)

    # 2
    pre = path1
    path1 = extra(path1, ['basic'], 'basic')
    if not path1:
        path1 = pre
    p(path1)
    
    # 3
    pre = path1
    path1 = extra(path1, ['android_log'], 'android_log')
    if not path1:
        path1 = pre
    p(path1)
    
    # 4
    for filename in os.listdir(path1):
        if filename.startswith('applog') or filename.startswith('kmsg'):
            filepath = os.path.join(path1, filename)
            extra2path(filepath, path1 + r"\out")
